<template>
  <div>
    <!-- 新增修改表单 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="DialogFormVisibleCom" :close-on-click-modal="false"
               :close-on-press-escape="false">
      <el-form ref="dataForm" :rules="rules" :model="formSubmitDataCom" label-position="right" label-width="140px"
               style="width: 90%; margin:10px auto;">

        <el-row :gutter="20">
          <el-col :span="12">
            <!-- 生成方案编号 -->
            <el-form-item label="生成方案编号：" prop="gs_ma_code">
              <el-input v-model="formSubmitDataCom.gs_ma_code" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 生成方案名称 -->
            <el-form-item label="生成方案名称：" prop="gs_ma_name">
              <el-input v-model="formSubmitDataCom.gs_ma_name"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 生成方案生效日期 -->
            <el-form-item label="生效日期：" prop="gs_ma_s_date">
              <el-date-picker
                v-model="formSubmitDataCom.gs_ma_s_date"
                type="date"
                placeholder="Please pick a date"
                value-format="yyyy-MM-dd"
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <!-- 生成方案失效日期 -->
            <el-form-item label="失效日期：" prop="gs_ma_e_date">
              <el-date-picker
                v-model="formSubmitDataCom.gs_ma_e_date"
                type="date"
                placeholder="Please pick a date"
                value-format="yyyy-MM-dd"
              />
            </el-form-item>
          </el-col>
          <!-- 生成曲线类型 -->
          <el-col :span="12">
            <el-form-item label="生成曲线类型" prop="gs_cv_type">
              <el-select v-model="formSubmitDataCom.gs_cv_type" class="filter-item" placeholder="Please select"
                         clearable>
                <el-option v-for="item in movementCurveType" :key="item.code" :label="item.name" :value="item.code"/>
              </el-select>

            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 生成曲线名称 -->
            <el-form-item label="生成曲线名称：" prop="gs_cv_name">
              <el-input v-model="formSubmitDataCom.gs_cv_name"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 移动平均天数 -->
            <el-form-item label="移动平均天数：" prop="ma_days">
              <el-input v-model="formSubmitDataCom.ma_days" type="number"/>
            </el-form-item>
          </el-col>
          <!-- 移动平均天数类型 -->
          <el-col :span="12">
            <el-form-item label="移动平均天数类型" prop="ma_dsys_type">
              <el-select v-model="formSubmitDataCom.ma_dsys_type" class="filter-item" placeholder="Please select"
                         @change="countryCode">
                <el-option v-for="item in curveType" :key="item.code" :label="item.name" :value="item.code"/>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 国家编号 -->
          <el-col :span="12" v-if="formSubmitDataCom.ma_dsys_type ==2 ">
            <el-form-item label="国家编号" required>
              <el-select v-model="formSubmitDataCom.country_number" class="filter-item" placeholder="Please select"
                         @change="nameExchange">
                <el-option v-for="item in countryList" :key="item.country" :label="item.country" :value="item.country"/>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 交易所名称 -->
          <el-col :span="12" v-if="formSubmitDataCom.ma_dsys_type ==2 ">
            <el-form-item label="交易所名称" required>
              <el-select v-model="formSubmitDataCom.exchange_type" class="filter-item" placeholder="Please select">
                <el-option v-for="item in exchangeList" :key="item.exchange_name" :label="item.exchange_name"
                           :value="item.exchange_name"/>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 国家编号 -->
          <el-col :span="12" v-if="formSubmitDataCom.ma_dsys_type == 3 ">
            <el-form-item label="国家编号" required>
              <el-select v-model="formSubmitDataCom.country_number" class="filter-item" placeholder="Please select">
                <el-option v-for="item in countryList" :key="item.country" :label="item.country" :value="item.country"/>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 移动平均曲线类型 -->
          <el-col :span="12">
            <el-form-item label="移动平均曲线类型" prop="ma_cv_type">
              <el-select v-model="formSubmitDataCom.ma_cv_type" class="filter-item" placeholder="Please select"
                         clearable @change="getMovementCurveType(formSubmitDataCom.ma_cv_type)">
                <el-option v-for="item in movementCurveType" :key="item.code" :label="item.name" :value="item.code"/>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 移动平均曲线 -->
          <el-col :span="12">
            <el-form-item label="移动平均曲线" prop="ma_cv_code">
              <el-select v-model="formSubmitDataCom.ma_cv_code" class="filter-item" placeholder="Please select"
                         clearable :disabled="!formSubmitDataCom.ma_cv_type">
                <el-option v-for="item in movingAverageCurve" :key="item.cv_code" :label="item.cv_name"
                           :value="item.cv_code"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button @click="resetFormData">
          重置
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          提交
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// import PageHeaderLayout from '@/layouts/PageHeaderLayout'
// import ApeDrawer from '@/components/ApeDrawer'
// import { mapGetters } from 'vuex'
// const calendarTypeOptions = [
//   { key: 'lp_code', display_name: 'lp_name' },
// ]

import XEUtils from 'xe-utils'
import {findmadsystype, findcvtype, findcvlist, findxiala, findexchange} from '../service/baseApi'
import resetFormSubmitData from '@/views/mixins/resetFormSubmitData'

export default {
  name: 'submitForm',
  mixins: [resetFormSubmitData],
  components: {
    //PageHeaderLayout,
  },
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false
    },
    dialogStatus: {
      type: String,
    },
    formSubmitData: {
      type: Object,
      default: function () {
        return {
          id: undefined,
          gs_ma_code: '',
          gs_ma_name: '',
          gs_ma_s_date: '',
          gs_ma_e_date: '',
          gs_cv_type: '',
          gs_cv_name: '',
          ma_days: '',
          ma_dsys_type: '',
          ma_cv_type: '',
          ma_cv_code: '',
          country_number: '',
          exchange_type: '',
          type: '',
          remark: '',
        }
      }
    },
  },
  // 定义属性
  data() {
    /* 生成方案生效日期 */
    var validateDateRangeSdate = (rule, value, callback) => {
      if (XEUtils.isEmpty(value)) {
        callback(new Error('生效日期必填!'));
      } else if ((!XEUtils.isEmpty(this.formSubmitDataCom.gs_ma_e_date)) && (value > this.formSubmitDataCom.gs_ma_e_date)) {
        callback(new Error('生效日期必须小于失效日期!'));
      } else {
        callback();
      }
    };
    /* 生成方案失效日期 */
    var validateDateRangeEdate = (rule, value, callback) => {
      if (XEUtils.isEmpty(value)) {
        callback(new Error('失效日期必填!'));
      } else if ((!XEUtils.isEmpty(this.formSubmitDataCom.gs_ma_s_date)) && (value < this.formSubmitDataCom.gs_ma_s_date)) {
        callback(new Error('失效日期必须大于生效日期!'));
      } else {
        callback();
      }
    };
    return {
      textMap: {
        update: '修改',
        create: '新增'
      },
      rules: {
        gs_ma_code: [{required: true, message: '生成方案编号必填', trigger: 'change'}],
        gs_ma_name: [{required: true, message: '生成方案名称必填', trigger: 'change'}],
        gs_ma_s_date: [{required: true, validator: validateDateRangeSdate, trigger: 'change'}],
        gs_ma_e_date: [{required: true, validator: validateDateRangeEdate, trigger: 'change'}],
        gs_cv_type: [{required: true, message: '生成曲线类型必填', trigger: 'change'}],
        gs_cv_name: [{required: true, message: '生成曲线名称必填', trigger: 'change'}],
        ma_days: [{required: true, message: '移动平均天数必填', trigger: 'change'}],
        ma_dsys_type: [{required: true, message: '移动平均天数类型必填', trigger: 'change'}],
        ma_cv_type: [{required: true, message: '移动平均曲线类型必填', trigger: 'change'}],
        ma_cv_code: [{required: true, message: '移动平均曲线必填', trigger: 'change'}],
      },
      curveType: [],//移动平均天数类型
      movementCurveType: [],//生成曲线类型
      movingAverageCurve: [],//移动平均曲线
      countryList: [], //国家编号下拉
      exchangeList: [], //交易所下拉
    }
  },
  // 计算属性，会监听依赖属性值随之变化
  computed: {
    DialogFormVisibleCom: {
      get() {
        return this.dialogFormVisible
      },
      set(val) {
        this.$emit('update:dialogFormVisible', val)
      }
    },
    formSubmitDataCom: {
      get() {
        return this.formSubmitData
      },
      set(val) {
        this.$emit('update:formSubmitData', val)
      }
    }
  },
  // 监控data中的数据变化
  watch: {
    "formSubmitDataCom.gs_ma_code"() {
      this.getDropDownData(this.formSubmitDataCom.ma_cv_type)
    },
    "DialogFormVisibleCom"() {
      if (this.dialogStatus == 'update') {
        this.countryCodes(this.formSubmitData.ma_dsys_type)
      }
    }
  },
  // 方法集合
  methods: {
    //用于修改回显
    async countryCodes(val) {
      await findxiala({ma_dsys_type: val}).then(({data}) => {
        this.countryList = data.rows
      })
      await findexchange({country: this.formSubmitDataCom.country_number}).then(({data}) => {
        this.exchangeList = data.rows
      })
    },

    // 获取下拉列表
    getDropDownData(val) {
      findcvlist({ma_cv_type: val}).then(({data}) => {
        this.movingAverageCurve = data.rows
      })
    },
    // 获取国家编号下拉列表
    countryCode(val) {
      if (val != 1) {
        findxiala({ma_dsys_type: val}).then(({data}) => {
          this.countryList = data.rows
          this.resetFormSubmitData({
            resetObj: Object.assign({}, this.formSubmitDataCom),
            replaceObj: {
              country_number: data.rows[0].country
            }
          })
          this.nameExchange(this.formSubmitDataCom.country_number)
        })
      }
    },
    //获取交易所名称下拉列表
    nameExchange(val) {
      if (this.formSubmitDataCom.ma_dsys_type == 2) {
        findexchange({country: val}).then(({data}) => {
          this.exchangeList = data.rows
          this.resetFormSubmitData({
            resetObj: Object.assign({}, this.formSubmitDataCom),
            replaceObj: {
              exchange_type: data.rows[0].exchange_name
            }
          })
        })
      }
    },

    // 根据移动平均曲线类型获取移动平均曲线
    getMovementCurveType(val) {
      this.resetFormSubmitData({
        resetObj: Object.assign({}, this.formSubmitDataCom),
        replaceObj: {
          ma_cv_code: ''
        }
      })
      if (val) {
        this.getDropDownData(val)
      } else {
        this.movingAverageCurve = []
      }
    },
    // 获取移动平均曲线下拉数据
    getlist() {
      findmadsystype().then(({data}) => {
        this.curveType = data.rows
      })
      findcvtype().then(({data}) => {
        this.movementCurveType = data.rows
      })
    },
    // 重置
    resetFormData() {
      let exclude = []
      if (this.dialogStatus === 'create') {
        exclude = [
          'gs_ma_code',
        ]
      }
      if (this.dialogStatus === 'update') {
        exclude = [
          'id',
          'gs_ma_code'
        ]
      }
      this.$emit('resetFormDataEmit', {exclude})
    },
    // 新增
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.formSubmitDataCom.ma_dsys_type == 1) {
            this.$delete(this.formSubmitData, 'exchange_type')
            this.$delete(this.formSubmitData, 'country_number')
            this.$emit('handleFromCreateDataEmit')
          } else if (this.formSubmitDataCom.ma_dsys_type == 3) {
            this.$delete(this.formSubmitData, 'exchange_type')
            this.$emit('handleFromCreateDataEmit')
          } else {
            this.$emit('handleFromCreateDataEmit')
          }

        }
      })
    },

    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.formSubmitDataCom.ma_dsys_type == 3) {
            this.$delete(this.formSubmitData, 'exchange_type')
            this.$emit('handleFromUpdateDataEmit')
          } else {
            this.$emit('handleFromUpdateDataEmit')
          }
        }
      })
    },

  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getlist()
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {

  },
  beforeCreate() {
  }, // 生命周期 - 创建之前
  beforeMount() {
  }, // 生命周期 - 挂载之前
  beforeUpdate() {
  }, // 生命周期 - 更新之前
  updated() {
  }, // 生命周期 - 更新之后
  beforeDestroy() {
  }, // 生命周期 - 销毁之前
  destroyed() {
  }, // 生命周期 - 销毁完成
  activated() {
  }, // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style lang='scss' scoped>

</style>
